package com.webc.framework.bean;

import com.webc.framework.annotation.RestController;
import com.webc.framework.annotation.Service;
import com.webc.framework.start.BeanFactory;
import com.webc.framework.start.LaunchClassloader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author hejianglong
 * @date 2018/9/6.
 */
public class BeanContainer {

    private static final Map<String, Object> BEAN_CONTAINER = new HashMap<String, Object>();

    static {
        List<Class<?>> classes = LaunchClassloader.getClasses();
        classes.stream()
                .filter(cl -> cl.isAnnotationPresent(RestController.class) || cl.isAnnotationPresent(Service.class))
                .forEach(aClass -> BEAN_CONTAINER.put(aClass.getName(), BeanFactory.newInstance(aClass)));
    }

    public static Map<String, Object> getBeanContainer() {
        return BEAN_CONTAINER;
    }

    public static Object getBean(String className) {
        for(Map.Entry entrySet: BEAN_CONTAINER.entrySet()) {
            if(entrySet.getKey().equals(className)) {
                return entrySet.getValue();
            }
        }
        return null;
    }

}
